//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package location;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import commands.ClaimRoadsideShopStand;
import commands.ForestsRemove;
import commands.ForestsUseTool;
import commands.HarvestField;
import commands.HarvestFieldGain;
import commands.HarvestFieldOver;
import commands.NeighborsSellAccept;
import commands.NeighborsSellCancel;
import commands.NewspaperAdvertise;
import commands.PutStuffToRoadsideShopStand;
import commands.SowSeeds;
import commands.SpeedUpCrops;
import commands.UpgradeBuilding;
import common.GlobalID;
import common.Logger;
import common.RoadsideShop;
import common.RoadsideShopSlot;
import common.Utils;
import init.StaticData;
import messages.EndClientTurn;
import messages.OwnHomeData;
import objects.AnimalEntry;
import objects.Animals;
import objects.AnimalsProduction;
import objects.BakeryProduction;
import objects.BarbecueGrillProduction;
import objects.CakeOvenProduction;
import objects.CandleMakerProduction;
import objects.CandyMachineProduction;
import objects.CoffeProduction;
import objects.ConstructionObject;
import objects.DairyProduction;
import objects.ExpLevels;
import objects.FactoryEntry;
import objects.FeedMillProduction;
import objects.Field;
import objects.FieldsEntry;
import objects.FishingProduction;
import objects.Forests;
import objects.ForestsEntry;
import objects.ForestsInstance;
import objects.ForestsInstanceEntry;
import objects.FruitEntry;
import objects.FruitTreeEntry;
import objects.FruitTrees;
import objects.Fruits;
import objects.FruitsProduction;
import objects.HatMakerProduction;
import objects.HoneyExtractorProduction;
import objects.HoneycombProduction;
import objects.HotdogStandProduction;
import objects.IceCreamMakerProduction;
import objects.JamMakerProduction;
import objects.JewelerProduction;
import objects.JuicePressProduction;
import objects.LoomProduction;
import objects.MineProduction;
import objects.MysteryBox;
import objects.MysteryBoxState;
import objects.NectarProduction;
import objects.NectarTreeEntry;
import objects.NectarTrees;
import objects.Neighbor;
import objects.OrderEntry;
import objects.Orders;
import objects.PastaKitchenProduction;
import objects.PastaMakerProduction;
import objects.PieOvenProduction;
import objects.PopcornPotProduction;
import objects.ProcessingBuildings;
import objects.ProductionFactories;
import objects.ProductionsGoodEntry;
import objects.SaladBarProduction;
import objects.SandwichBarProduction;
import objects.SauceMixerProduction;
import objects.SewingMachineProduction;
import objects.Silos;
import objects.SilosBarnMaterials;
import objects.SmeltersProduction;
import objects.SmoothieMixerProduction;
import objects.SoupKitchenProduction;
import objects.SugarMillProduction;
import objects.SushiBarProduction;
import objects.WareHouse;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

public class Farm extends LocationBase {
	private static final int adTime = 300;
	private List<DisplayObject> Fields;
	private Integer FreeFieldID;
	private Integer FreeFruitTreeID;
	private Integer FreeNectarTreeID;
	private Integer FreeConstructionID;
	private RoadsideShop shop;
	private List<Neighbor> neighbors = new ArrayList();
	private DisplayObjectTimer freeAdTimer;
	private int currProductionInstance = 0;
	private boolean chickenHabitatPresent;
	private LinkedHashMap<Integer, Integer> readyToCompleteBuildingExp = new LinkedHashMap();
	private LinkedHashMap<Integer, ConstructionObject> constructionBuildings = new LinkedHashMap();
	private MineProduction mine = new MineProduction();
	private SmeltersProduction Smelters = new SmeltersProduction();
	private JewelerProduction Jeweler = new JewelerProduction();
	private SugarMillProduction SugarMill = new SugarMillProduction();
	private FeedMillProduction FeedMill = new FeedMillProduction();
	private JamMakerProduction JamMaker = new JamMakerProduction();
	private JuicePressProduction JuicePress = new JuicePressProduction();
	private BakeryProduction Bakery = new BakeryProduction();
	private CoffeProduction CoffeKiosk = new CoffeProduction();
	private BarbecueGrillProduction Grill = new BarbecueGrillProduction();
	private CakeOvenProduction CakeOven = new CakeOvenProduction();
	private CandyMachineProduction CandyMachine = new CandyMachineProduction();
	private IceCreamMakerProduction IceCream = new IceCreamMakerProduction();
	private LoomProduction Loom = new LoomProduction();
	private PieOvenProduction PieOven = new PieOvenProduction();
	private PopcornPotProduction Popcorn = new PopcornPotProduction();
	private SaladBarProduction SaladBar = new SaladBarProduction();
	private SandwichBarProduction SandwichBar = new SandwichBarProduction();
	private SauceMixerProduction SauceMixer = new SauceMixerProduction();
	private SewingMachineProduction SewingMachine = new SewingMachineProduction();
	private SmoothieMixerProduction SmoothieMixer = new SmoothieMixerProduction();
	private SoupKitchenProduction SoupKitchen = new SoupKitchenProduction();
	private SushiBarProduction SushiBar = new SushiBarProduction();
	private HoneyExtractorProduction HoneyExtractor = new HoneyExtractorProduction();
	private DairyProduction Dairy = new DairyProduction();
	private FishingProduction Fishing = new FishingProduction();
	private HoneycombProduction Honeycomb = new HoneycombProduction();
	private CandleMakerProduction CandleMaker = new CandleMakerProduction();
	private HatMakerProduction HatMaker = new HatMakerProduction();
	private PastaMakerProduction PastaMaker = new PastaMakerProduction();
	private HotdogStandProduction HotdogStand = new HotdogStandProduction();
	private PastaKitchenProduction PastaKitchen = new PastaKitchenProduction();
	private final LinkedHashMap<String, AnimalsProduction> animals = new LinkedHashMap();
	private final LinkedHashMap<String, FruitsProduction> fruits = new LinkedHashMap();
	private final LinkedHashMap<String, NectarProduction> nectar = new LinkedHashMap();
	private LinkedHashMap<Integer, ProductionFactories> productions = new LinkedHashMap();
	private LinkedHashMap<Integer, ProductionFactories> activeProductions = new LinkedHashMap();
	private ForestsInstance forests = new ForestsInstance();
	private final Orders orders = new Orders();
	private MysteryBox OpenBox = null;
	private int LastAdvertiseIndex = 0;
	private int harvestCount = 0;

	public Farm(OwnHomeData hd, String profileName) {
		super(hd.getInventory(), hd.HomeData, profileName);
		this.initProductions();
		this.Fields = new ArrayList();
		this.shop = hd.getShop();
	}

	public Farm(LinkedHashMap<Integer, Integer> inventory, String homeData, RoadsideShop shop, String profileName) {
		super(inventory, homeData, profileName);
		this.initProductions();
		this.Fields = new ArrayList();
		this.shop = shop;
	}

	public void update(OwnHomeData hd) {
		this.inventory = hd.getInventory();
		this.locationObjects = hd.HomeData;
		this.shop = hd.getShop();
	}

	public void update(LinkedHashMap<Integer, Integer> inventory, String homeData, RoadsideShop shop) {
		this.inventory = inventory;
		this.locationObjects = homeData;
		this.shop = shop;
	}

	public AnimalsProduction getAnimalProduction(String animal) {
		return (AnimalsProduction)this.animals.get(animal);
	}

	public int getNextAnimalInstance() {
		int instance = 0;

		AnimalsProduction ap;
		for(Iterator var3 = this.animals.values().iterator(); var3.hasNext(); instance += ap.size()) {
			ap = (AnimalsProduction)var3.next();
		}

		return instance;
	}

	public BakeryProduction getBakery() {
		return this.Bakery;
	}

	public FeedMillProduction getHammermill() {
		return this.FeedMill;
	}

	public DairyProduction getDairy() {
		return this.Dairy;
	}

	public ProductionFactories getProductionById(Integer id) {
		return (ProductionFactories)this.productions.get(id);
	}

	public Orders getOrders() {
		return this.orders;
	}

	public boolean chickenHabitatPresent() {
		return this.chickenHabitatPresent;
	}

	private void createActiveProductionFactories() {
		this.activeProductions.clear();
		Iterator var2 = this.productions.keySet().iterator();

		while(true) {
			Integer id;
			ProductionFactories pf;
			while(true) {
				if(!var2.hasNext()) {
					return;
				}

				id = (Integer)var2.next();
				pf = (ProductionFactories)this.productions.get(id);
				FactoryEntry fe = ProcessingBuildings.getById(id.intValue());
				if(fe != null) {
					if(this.currentLevel.intValue() < fe.getUnlockLevel()) {
						continue;
					}
					break;
				} else {
					AnimalEntry a = Animals.getByName(pf.getProductionName());
					if(a != null) {
						if(this.currentLevel.intValue() < a.getUnlockLevel().intValue()) {
							continue;
						}
						break;
					} else {
						FruitTreeEntry ft = FruitTrees.getByName(pf.getProductionName());
						if(ft != null) {
							FruitEntry n1 = Fruits.getByName(ft.getFruit());
							if(n1 == null || this.currentLevel.intValue() >= n1.getUnlockLevel().intValue()) {
								break;
							}
						} else {
							NectarTreeEntry n = NectarTrees.getByName(pf.getProductionName());
							if(n != null) {
								ProductionsGoodEntry pge = n.getGoodByName(n.getFruit());
								if(pge != null && this.currentLevel.intValue() >= pge.getUnlockLevel().intValue()) {
									break;
								}
							}
						}
					}
				}
			}

			pf.logEnable(true);
			this.activeProductions.put(id, pf);
		}
	}

	public void constructTutorialPlanningList(int level) {
		Iterator var3 = this.activeProductions.values().iterator();

		while(var3.hasNext()) {
			ProductionFactories pf = (ProductionFactories)var3.next();
			LinkedHashMap pl = new LinkedHashMap();
			Iterator var6 = pf.getGoods().iterator();

			while(var6.hasNext()) {
				ProductionsGoodEntry pge = (ProductionsGoodEntry)var6.next();
				if(pf.checkComponentsUnlockLevel(pge, Integer.valueOf(level)) && !pge.getName().equals("Cow Food")) {
					pl.put(pge.getName(), Integer.valueOf(1));
				}
			}

			pf.setGoodsPlanningList(pl);
		}

	}

	public void initProductions() {
		Iterator fp = Animals.get().iterator();

		while(fp.hasNext()) {
			AnimalEntry fe = (AnimalEntry)fp.next();
			this.animals.put(fe.getName(), new AnimalsProduction(fe.getName()));
		}

		fp = FruitTrees.get().iterator();

		while(fp.hasNext()) {
			FruitTreeEntry fe1 = (FruitTreeEntry)fp.next();
			this.fruits.put(fe1.getName(), new FruitsProduction(fe1.getName()));
		}

		fp = NectarTrees.get().iterator();

		while(fp.hasNext()) {
			NectarTreeEntry fe2 = (NectarTreeEntry)fp.next();
			this.nectar.put(fe2.getName(), new NectarProduction(fe2.getName()));
		}

		this.productions.clear();
		FactoryEntry fe3 = ProcessingBuildings.getByName(this.Smelters.getProductionName());
		this.productions.put(Integer.valueOf(fe3.getId()), this.Smelters);
		fe3 = ProcessingBuildings.getByName(this.Jeweler.getProductionName());
		this.productions.put(Integer.valueOf(fe3.getId()), this.Jeweler);
		fe3 = ProcessingBuildings.getByName(this.SugarMill.getProductionName());
		this.productions.put(Integer.valueOf(fe3.getId()), this.SugarMill);
		fe3 = ProcessingBuildings.getByName(this.FeedMill.getProductionName());
		this.productions.put(Integer.valueOf(fe3.getId()), this.FeedMill);
		fe3 = ProcessingBuildings.getByName(this.Dairy.getProductionName());
		this.productions.put(Integer.valueOf(fe3.getId()), this.Dairy);
		fe3 = ProcessingBuildings.getByName(this.JamMaker.getProductionName());
		this.productions.put(Integer.valueOf(fe3.getId()), this.JamMaker);
		fe3 = ProcessingBuildings.getByName(this.JuicePress.getProductionName());
		this.productions.put(Integer.valueOf(fe3.getId()), this.JuicePress);
		fe3 = ProcessingBuildings.getByName(this.Bakery.getProductionName());
		this.productions.put(Integer.valueOf(fe3.getId()), this.Bakery);
		fe3 = ProcessingBuildings.getByName(this.CoffeKiosk.getProductionName());
		this.productions.put(Integer.valueOf(fe3.getId()), this.CoffeKiosk);
		fe3 = ProcessingBuildings.getByName(this.Grill.getProductionName());
		this.productions.put(Integer.valueOf(fe3.getId()), this.Grill);
		fe3 = ProcessingBuildings.getByName(this.CakeOven.getProductionName());
		this.productions.put(Integer.valueOf(fe3.getId()), this.CakeOven);
		fe3 = ProcessingBuildings.getByName(this.CandyMachine.getProductionName());
		this.productions.put(Integer.valueOf(fe3.getId()), this.CandyMachine);
		fe3 = ProcessingBuildings.getByName(this.IceCream.getProductionName());
		this.productions.put(Integer.valueOf(fe3.getId()), this.IceCream);
		fe3 = ProcessingBuildings.getByName(this.Loom.getProductionName());
		this.productions.put(Integer.valueOf(fe3.getId()), this.Loom);
		fe3 = ProcessingBuildings.getByName(this.PieOven.getProductionName());
		this.productions.put(Integer.valueOf(fe3.getId()), this.PieOven);
		fe3 = ProcessingBuildings.getByName(this.Popcorn.getProductionName());
		this.productions.put(Integer.valueOf(fe3.getId()), this.Popcorn);
		fe3 = ProcessingBuildings.getByName(this.SaladBar.getProductionName());
		this.productions.put(Integer.valueOf(fe3.getId()), this.SaladBar);
		fe3 = ProcessingBuildings.getByName(this.SandwichBar.getProductionName());
		this.productions.put(Integer.valueOf(fe3.getId()), this.SandwichBar);
		fe3 = ProcessingBuildings.getByName(this.SauceMixer.getProductionName());
		this.productions.put(Integer.valueOf(fe3.getId()), this.SauceMixer);
		fe3 = ProcessingBuildings.getByName(this.SewingMachine.getProductionName());
		this.productions.put(Integer.valueOf(fe3.getId()), this.SewingMachine);
		fe3 = ProcessingBuildings.getByName(this.SmoothieMixer.getProductionName());
		this.productions.put(Integer.valueOf(fe3.getId()), this.SmoothieMixer);
		fe3 = ProcessingBuildings.getByName(this.SoupKitchen.getProductionName());
		this.productions.put(Integer.valueOf(fe3.getId()), this.SoupKitchen);
		fe3 = ProcessingBuildings.getByName(this.SushiBar.getProductionName());
		this.productions.put(Integer.valueOf(fe3.getId()), this.SushiBar);
		fe3 = ProcessingBuildings.getByName(this.HoneyExtractor.getProductionName());
		this.productions.put(Integer.valueOf(fe3.getId()), this.HoneyExtractor);
		fe3 = ProcessingBuildings.getByName(this.CandleMaker.getProductionName());
		this.productions.put(Integer.valueOf(fe3.getId()), this.CandleMaker);
		fe3 = ProcessingBuildings.getByName(this.HatMaker.getProductionName());
		this.productions.put(Integer.valueOf(fe3.getId()), this.HatMaker);
		fe3 = ProcessingBuildings.getByName(this.PastaMaker.getProductionName());
		this.productions.put(Integer.valueOf(fe3.getId()), this.PastaMaker);
		fe3 = ProcessingBuildings.getByName(this.HotdogStand.getProductionName());
		this.productions.put(Integer.valueOf(fe3.getId()), this.HotdogStand);
		fe3 = ProcessingBuildings.getByName(this.PastaKitchen.getProductionName());
		this.productions.put(Integer.valueOf(fe3.getId()), this.PastaKitchen);
		Iterator var3 = this.animals.values().iterator();

		while(var3.hasNext()) {
			AnimalsProduction fp1 = (AnimalsProduction)var3.next();
			AnimalEntry f = Animals.getByName(fp1.getProductionName());
			this.productions.put(f.getGlobalID(), fp1);
		}

		var3 = this.nectar.values().iterator();

		while(var3.hasNext()) {
			NectarProduction fp2 = (NectarProduction)var3.next();
			NectarTreeEntry f1 = NectarTrees.getByName(fp2.getProductionName());
			this.productions.put(f1.getGlobalID(), fp2);
		}

		var3 = this.fruits.values().iterator();

		while(var3.hasNext()) {
			FruitsProduction fp3 = (FruitsProduction)var3.next();
			FruitTreeEntry f2 = FruitTrees.getByName(fp3.getProductionName());
			this.productions.put(f2.getGlobalID(), fp3);
		}

		this.productions.put(Integer.valueOf(GlobalID.create(this.Honeycomb.getObjectClass(), 0)), this.Honeycomb);
		this.constructRequirementComponentPairs(false);
	}

	private void constructRequirementComponentPairs(boolean showlog) {
		Iterator var3 = this.productions.values().iterator();

		while(true) {
			ProductionFactories pf;
			do {
				if(!var3.hasNext()) {
					return;
				}

				pf = (ProductionFactories)var3.next();
			} while(pf instanceof FruitsProduction);

			Iterator var5 = pf.getGoods().iterator();

			label39:
			while(var5.hasNext()) {
				ProductionsGoodEntry pge = (ProductionsGoodEntry)var5.next();
				Iterator var7 = pge.getRequirements().keySet().iterator();

				while(true) {
					while(true) {
						if(!var7.hasNext()) {
							continue label39;
						}

						String reqName = (String)var7.next();
						Integer reqId = StaticData.getGlobalId(reqName);
						if(reqId != null && GlobalID.getClassID(reqId.intValue()) != 0) {
							pf.addReqirementComponentPair(reqName, reqId);
						} else if(showlog) {
							System.out.println("addReqirementComponentPair(" + reqName + ", null)");
						}
					}
				}
			}
		}
	}

	public void reload(boolean quiet) {
		this.Fields.clear();
		this.neighbors.clear();
		this.forests.reset();
		this.updateCurrentResources();
		this.clearProductionData();
		this.createActiveProductionFactories();
		this.readyToCompleteBuildingExp.clear();
		this.constructionBuildings.clear();
		this.OpenBox = null;

		try {
			this.currProductionInstance = 0;
			int e = 0;
			int currFruitInstance = 0;
			int currentNectarInstance = 0;
			int beehiveInstance = 0;
			int currentForestsInstance = 0;
			JsonParser jp = new JsonParser();
			JsonElement jpe = jp.parse(this.locationObjects);
			if(jpe != null && jpe.isJsonObject()) {
				JsonObject jo = jpe.getAsJsonObject();
				JsonArray objs = jo.getAsJsonArray("Objects");
				Iterator var12 = objs.iterator();

				while(var12.hasNext()) {
					JsonElement pf = (JsonElement)var12.next();
					int oId = pf.getAsJsonObject().getAsJsonPrimitive("ID").getAsInt();
					switch(GlobalID.getClassID(oId)) {
						case 2:
							this.checkCars(pf);
							break;
						case 4:
							this.checkFields(pf);
							break;
						case 5:
							this.checkForests(pf, currentForestsInstance);
							++currentForestsInstance;
							break;
						case 6:
							this.checkProduction(pf, this.currProductionInstance);
							++this.currProductionInstance;
							break;
						case 11:
							if(GlobalID.getInstanceID(oId) == 0) {
								this.chickenHabitatPresent = true;
							}
							break;
						case 13:
							this.checkAnimals(pf, e);
							++e;
							break;
						case 21:
							this.checkConstruction(pf);
							break;
						case 32:
							this.checkOrders(pf);
							break;
						case 35:
							this.checkFreeAdTimer(pf);
							break;
						case 49:
							this.checkNeighbors(pf);
							break;
						case 53:
							this.checkMysteryBox(pf);
							break;
						case 62:
							this.checkFruits(pf, currFruitInstance);
							++currFruitInstance;
							break;
						case 63:
							this.checkMine(pf);
							break;
						case 147:
							this.checkHoneycomb(pf, beehiveInstance);
							++beehiveInstance;
							break;
						case 148:
							this.checkNectars(pf, currentNectarInstance);
							++currentNectarInstance;
					}
				}

				this.FreeFieldID = Integer.valueOf(GlobalID.create(4, this.Fields.size()));
				this.FreeFruitTreeID = Integer.valueOf(GlobalID.create(62, this.getFruitTreesCount().intValue() - 1));
				this.FreeNectarTreeID = Integer.valueOf(GlobalID.create(148, this.getNectarTreesCount().intValue() - 1));
				this.FreeConstructionID = Integer.valueOf(GlobalID.create(21, this.readyToCompleteBuildingExp.size() - 1));
				var12 = this.activeProductions.values().iterator();

				while(var12.hasNext()) {
					ProductionFactories var15 = (ProductionFactories)var12.next();
					if(!quiet && var15.logEnable()) {
						System.out.println(Logger.formatP(this.logName, "  " + var15.getProductionName() + ": ready:" + var15.getReadyCount() + " empty:" + var15.getEmptyCount() + " inprogress:" + var15.getInProgressCount()));
					}
				}

				this.constructTutorialPlanningList(this.currentLevel.intValue());
			}
		} catch (Exception var14) {
			var14.printStackTrace();
		}

		if(!quiet) {
			this.shop.setPrintNotEmptySlots(true);
			System.out.println(Logger.formatP(this.logName, "  " + this.shop));
			System.out.println(Logger.formatP(this.logName, "  --- The Contents of Barn ---"));
			this.showBarnContent(this.inventory);
			System.out.println(Logger.formatP(this.logName, "  --- The Contents of Silos ---"));
			this.showSilosContent(this.inventory);
			System.out.println(Logger.formatP(this.logName, "  --- The Contents of Table 19 ---"));
			this.showTable19Content(this.inventory);
			this.showTools(this.inventory);
			this.showMaterials(this.inventory);
			this.showFields();
			System.out.println(Logger.formatP(this.logName, "  SILOS: " + this.getSilosCount(this.inventory) + "/" + this.getSilosMax(this.inventory)));
			System.out.println(Logger.formatP(this.logName, "  BARN: " + this.getBarnCount(this.inventory) + "/" + this.getBarnMax(this.inventory)));
			System.out.println(Logger.formatP(this.logName, "  Level:" + this.currentLevel + ", Experience:" + this.currentExp + "/" + ExpLevels.getExpToNextLevel(this.currentLevel.intValue()) + ", Coins:" + this.currentCoins + ", Crystalls:" + this.currentCrystalls));
		}

	}

	private Integer getFruitTreesCount() {
		Integer val = Integer.valueOf(0);

		FruitsProduction fp;
		for(Iterator var3 = this.fruits.values().iterator(); var3.hasNext(); val = Integer.valueOf(val.intValue() + fp.initialSize())) {
			fp = (FruitsProduction)var3.next();
		}

		return val;
	}

	private Integer getNectarTreesCount() {
		Integer val = Integer.valueOf(0);

		NectarProduction np;
		for(Iterator var3 = this.nectar.values().iterator(); var3.hasNext(); val = Integer.valueOf(val.intValue() + np.initialSize())) {
			np = (NectarProduction)var3.next();
		}

		return val;
	}

	private void clearProductionData() {
		Iterator var2 = this.productions.values().iterator();

		while(var2.hasNext()) {
			ProductionFactories pf = (ProductionFactories)var2.next();
			pf.clear();
		}

	}

	private void checkCars(JsonElement e) {
	}

	private void checkForests(JsonElement e, int instance) {
		Integer oId = Integer.valueOf(e.getAsJsonObject().getAsJsonPrimitive("ID").getAsInt());
		JsonPrimitive xp = e.getAsJsonObject().getAsJsonPrimitive("X");
		JsonPrimitive yp = e.getAsJsonObject().getAsJsonPrimitive("Y");
		Integer X;
		if(xp != null && !xp.isJsonNull()) {
			X = Integer.valueOf(xp.getAsInt());
		} else {
			X = Integer.valueOf(0);
		}

		Integer Y;
		if(yp != null && !yp.isJsonNull()) {
			Y = Integer.valueOf(yp.getAsInt());
		} else {
			Y = Integer.valueOf(0);
		}

		ForestsInstanceEntry f = new ForestsInstanceEntry(oId, X, Y, Integer.valueOf(instance));
		this.forests.addEntry(f);
	}

	private void checkFields(JsonElement e) {
		int oId = e.getAsJsonObject().getAsJsonPrimitive("ID").getAsInt();
		int fId = this.Fields.size();
		DisplayObjectTimer timer = null;
		JsonObject jpr = e.getAsJsonObject().getAsJsonObject("Timer");
		DisplayObject hddo = new DisplayObject(oId, 400000 + fId);
		if(jpr != null && !jpr.isJsonNull() && jpr.isJsonObject()) {
			int ss = jpr.getAsJsonPrimitive("StartSeconds").getAsInt();
			int tl = jpr.getAsJsonPrimitive("TicksLeft").getAsInt();
			timer = new DisplayObjectTimer(ss, tl, 0);
			hddo.setTimer(timer);
		}

		this.Fields.add(fId, hddo);
	}

	private void checkNeighbors(JsonElement e) {
		int oId = e.getAsJsonObject().getAsJsonPrimitive("ID").getAsInt();
		int ggid = e.getAsJsonObject().getAsJsonPrimitive("GoodGlobalId").getAsInt();
		if(ggid > 0) {
			JsonPrimitive jpga = e.getAsJsonObject().getAsJsonPrimitive("GoodAmount");
			if(jpga != null && jpga.isJsonPrimitive() && jpga.isNumber()) {
				int ga = jpga.getAsInt();
				this.neighbors.add(new Neighbor(oId, ggid, ga));
			}
		}

	}

	private void checkFreeAdTimer(JsonElement e) {
		JsonObject p = e.getAsJsonObject().getAsJsonObject("FreeAdTimer");
		if(p != null && !p.isJsonNull() && p.isJsonObject()) {
			JsonPrimitive jpss = p.getAsJsonPrimitive("StartSeconds");
			JsonPrimitive jptl = p.getAsJsonPrimitive("TicksLeft");
			if(jpss != null && !jpss.isJsonNull() && jpss.isNumber() && jptl != null && !jptl.isJsonNull() && jptl.isNumber()) {
				this.freeAdTimer = new DisplayObjectTimer(jpss.getAsInt(), jptl.getAsInt(), 0);
				return;
			}
		}

		this.freeAdTimer = new DisplayObjectTimer(300, 0, 0);
	}

	private void checkConstruction(JsonElement e) {
		String contructionName = null;
		JsonObject p = e.getAsJsonObject();
		int constructionId = p.getAsJsonPrimitive("ID").getAsInt();
		JsonObject joct = p.getAsJsonObject("ConstructionTimer");
		JsonPrimitive jptd = p.getAsJsonPrimitive("TargetData");
		Integer targetID = null;
		if(jptd != null && !jptd.isJsonNull() && jptd.isNumber()) {
			targetID = Integer.valueOf(jptd.getAsInt());
			FactoryEntry jpss = ProcessingBuildings.getById(targetID.intValue());
			if(jpss != null) {
				contructionName = jpss.getName();
			}
		}

		if(contructionName == null) {
			contructionName = String.valueOf(constructionId);
		}

		if(targetID != null && joct != null && !joct.isJsonNull() && joct.isJsonObject()) {
			JsonPrimitive jpss1 = joct.getAsJsonPrimitive("StartSeconds");
			JsonPrimitive jptl = joct.getAsJsonPrimitive("TicksLeft");
			if(jpss1 != null && !jpss1.isJsonNull() && jpss1.isNumber() && jptl != null && !jptl.isJsonNull() && jptl.isNumber()) {
				ConstructionObject co = new ConstructionObject(targetID, new DisplayObjectTimer(jpss1.getAsInt(), jptl.getAsInt(), 0), Integer.valueOf(this.constructionBuildings.size()));
				this.constructionBuildings.put(targetID, co);
			}

		} else {
			if(targetID == null) {
				if(constructionId != 2100003) {
					System.out.println(Logger.formatPT(this.logName, "  Unsupported Construction ID: " + e.toString()));
					return;
				}

				targetID = Integer.valueOf(GlobalID.create(11, 0));
				contructionName = "Chicken Habitat";
			}

			this.readyToCompleteBuildingExp.put(Integer.valueOf(GlobalID.create(21, this.readyToCompleteBuildingExp.size())), targetID);
		}
	}

	private void checkMine(JsonElement e) {
		int oId = e.getAsJsonObject().getAsJsonPrimitive("ID").getAsInt();
		JsonObject m = e.getAsJsonObject();
		if(GlobalID.getInstanceID(oId) == 0) {
			this.mine.Lock();
		} else {
			this.mine.Unlock();
		}

		this.mine.setLevel(this.currentLevel.intValue());
		if(m != null && m.isJsonObject()) {
			JsonPrimitive jrs = m.getAsJsonPrimitive("RandomSeed");
			if(jrs != null && !jrs.isJsonNull()) {
				this.mine.initRandomVector(jrs.getAsInt());
			}

			JsonObject prodList = m.getAsJsonObject("ProductionList");
			if(prodList != null && !prodList.isJsonNull()) {
				JsonArray fpa = prodList.getAsJsonArray("FinishedProductions");
				if(fpa != null && fpa.isJsonArray()) {
					Iterator var8 = fpa.iterator();

					while(var8.hasNext()) {
						JsonElement je = (JsonElement)var8.next();
						int pId = je.getAsJsonObject().getAsJsonPrimitive("ID").getAsInt();
						this.mine.addFinished(Integer.valueOf(pId));
					}
				}
			}
		}

	}

	private void checkProduction(JsonElement e, int instance) {
		JsonObject p = e.getAsJsonObject();
		int oId = p.getAsJsonPrimitive("ID").getAsInt();
		FactoryEntry fe = ProcessingBuildings.getById(oId);
		if(fe != null) {
			ProductionFactories pcfg = (ProductionFactories)this.productions.get(Integer.valueOf(oId));
			if(pcfg != null) {
				int defaultSlots = fe.getSlots();
				int rank = 0;
				JsonPrimitive r = p.getAsJsonPrimitive("Rank");
				if(r != null && !r.isJsonNull() && r.isNumber()) {
					rank = r.getAsInt();
				}

				if(rank == 0) {
					rank = defaultSlots;
				} else {
					rank = rank + defaultSlots - 1;
				}

				DisplayProductionObject dpo = new DisplayProductionObject(oId, rank, instance);
				JsonObject prodList = p.getAsJsonObject("ProductionList");
				if(prodList != null && !prodList.isJsonNull()) {
					JsonArray pa = prodList.getAsJsonArray("Productions");
					if(pa != null && pa.isJsonArray()) {
						Iterator je = pa.iterator();

						while(je.hasNext()) {
							JsonElement fpa = (JsonElement)je.next();
							int pId = fpa.getAsJsonObject().getAsJsonPrimitive("ID").getAsInt();
							JsonObject pId1 = fpa.getAsJsonObject().getAsJsonObject("Timer");
							if(pId1 != null && !pId1.isJsonNull() && pId1.isJsonObject()) {
								int ss = pId1.getAsJsonPrimitive("StartSeconds").getAsInt();
								int tl = pId1.getAsJsonPrimitive("TicksLeft").getAsInt();
								dpo.addProductionTimer(pId, new DisplayObjectTimer(ss, tl, 0));
							}
						}
					}

					JsonArray fpa1 = prodList.getAsJsonArray("FinishedProductions");
					if(fpa1 != null && fpa1.isJsonArray()) {
						Iterator pId2 = fpa1.iterator();

						while(pId2.hasNext()) {
							JsonElement je1 = (JsonElement)pId2.next();
							int pId3 = je1.getAsJsonObject().getAsJsonPrimitive("ID").getAsInt();
							dpo.addFinished(pId3);
						}
					}
				}

				pcfg.add(dpo);
			}
		}
	}

	private void checkAnimals(JsonElement e, int instance) {
		JsonObject p = e.getAsJsonObject();
		int oId = p.getAsJsonPrimitive("ID").getAsInt();
		ProductionFactories pcfg = (ProductionFactories)this.productions.get(Integer.valueOf(oId));
		if(pcfg != null) {
			AnimalEntry ae = Animals.getById(Integer.valueOf(oId));
			if(ae != null) {
				AnimalsProduction ap = (AnimalsProduction)this.animals.get(ae.getName());
				if(ap != null) {
					DisplayProductionObject dpo = new DisplayProductionObject(oId, 1, instance);
					JsonPrimitive fed = p.getAsJsonPrimitive("Fed");
					if(fed != null && !fed.isJsonNull() && fed.isBoolean()) {
						ProductionsGoodEntry pge = ae.getGoodByName(ae.getGood());
						Integer pId = pge.getGlobalID(Integer.valueOf(ap.getObjectClass()));
						JsonObject jpr = p.getAsJsonObject("Timer");
						if(jpr != null && !jpr.isJsonNull() && jpr.isJsonObject()) {
							int ss = jpr.getAsJsonPrimitive("StartSeconds").getAsInt();
							int tl = jpr.getAsJsonPrimitive("TicksLeft").getAsInt();
							dpo.addProductionTimer(pId.intValue(), new DisplayObjectTimer(ss, tl, 0));
						} else {
							dpo.addFinished(pId.intValue());
						}
					}

					ap.add(dpo);
				}
			}

		}
	}

	private void checkFruits(JsonElement e, int instance) {
		JsonObject p = e.getAsJsonObject();
		int oId = p.getAsJsonPrimitive("ID").getAsInt();
		ProductionFactories pcfg = (ProductionFactories)this.productions.get(Integer.valueOf(oId));
		if(pcfg != null) {
			FruitTreeEntry treeEntry = FruitTrees.getById(Integer.valueOf(oId));
			if(treeEntry != null) {
				FruitEntry fruitEntry = Fruits.getByTree(treeEntry.getName());
				if(fruitEntry != null) {
					boolean isFruitsPresent = false;
					boolean isGrowTimerPresent = false;
					Integer stage = Integer.valueOf(0);
					Integer x = null;
					Integer y = null;
					Boolean mirrored = null;
					JsonPrimitive jnumber = p.getAsJsonPrimitive("X");
					if(jnumber != null && !jnumber.isJsonNull() && jnumber.isNumber()) {
						x = Integer.valueOf(jnumber.getAsInt());
					}

					jnumber = p.getAsJsonPrimitive("Y");
					if(jnumber != null && !jnumber.isJsonNull() && jnumber.isNumber()) {
						y = Integer.valueOf(jnumber.getAsInt());
					}

					jnumber = p.getAsJsonPrimitive("Mirrored");
					if(jnumber != null && !jnumber.isJsonNull() && jnumber.isBoolean()) {
						mirrored = Boolean.valueOf(jnumber.getAsBoolean());
					}

					JsonPrimitive jphase = p.getAsJsonPrimitive("Phase");
					if(jphase != null && !jphase.isJsonNull() && jphase.isNumber()) {
						stage = Integer.valueOf(jphase.getAsInt());
					}

					int rank = treeEntry.getFruitCount(stage.intValue()).intValue();
					DisplayProductionObject dpo = new DisplayProductionObject(oId, rank, instance);
					dpo.setStage(stage);
					if(x != null) {
						dpo.setX(x);
					}

					if(y != null) {
						dpo.setY(y);
					}

					if(mirrored != null) {
						dpo.setMirroed(mirrored);
					}

					JsonArray jafruits = p.getAsJsonArray("Fruits");
					if(jafruits != null && jafruits.isJsonArray() && !jafruits.isJsonNull()) {
						isFruitsPresent = true;
						Integer jpr = Integer.valueOf(0);

						for(Iterator tl = jafruits.iterator(); tl.hasNext(); jpr = Integer.valueOf(jpr.intValue() + 1)) {
							JsonElement fp = (JsonElement)tl.next();
							if(fp.getAsBoolean()) {
								dpo.addRemoved(fruitEntry.getGlobalID().intValue());
								dpo.addRemovedIndex(jpr.intValue());
							} else {
								dpo.addFinished(fruitEntry.getGlobalID().intValue());
								dpo.addFinishedIndex(jpr.intValue());
							}
						}
					}

					JsonObject var23 = p.getAsJsonObject("GrowTimer");
					if(var23 != null && !var23.isJsonNull() && var23.isJsonObject()) {
						isGrowTimerPresent = true;
						int var24 = var23.getAsJsonPrimitive("StartSeconds").getAsInt();
						int var26 = var23.getAsJsonPrimitive("TicksLeft").getAsInt();

						for(int i = 0; i < rank; ++i) {
							dpo.addProductionTimer(fruitEntry.getGlobalID().intValue(), new DisplayObjectTimer(var24, var26, 0));
							dpo.addFinishedIndex(i);
						}
					}

					FruitsProduction var25 = (FruitsProduction)this.fruits.get(treeEntry.getName());
					if(var25 != null) {
						if(!isFruitsPresent && !isGrowTimerPresent) {
							var25.addRedyToRemove(dpo);
						} else {
							var25.add(dpo);
						}
					}
				}
			}

		}
	}

	private void checkOrders(JsonElement e) {
		this.orders.clear();
		JsonObject p = e.getAsJsonObject();
		JsonArray jOrders = p.getAsJsonArray("Orders");
		if(jOrders != null && !jOrders.isJsonNull() && jOrders.isJsonArray()) {
			Iterator var5 = jOrders.iterator();

			label53:
			while(true) {
				while(true) {
					JsonElement je;
					JsonPrimitive jL;
					do {
						do {
							do {
								if(!var5.hasNext()) {
									break label53;
								}

								je = (JsonElement)var5.next();
								jL = je.getAsJsonObject().getAsJsonPrimitive("Lvl");
							} while(jL == null);
						} while(jL.isJsonNull());
					} while(!jL.isNumber());

					Integer level = Integer.valueOf(jL.getAsInt());
					JsonPrimitive jR = je.getAsJsonObject().getAsJsonPrimitive("Receiver");
					if(jR != null && !jR.isJsonNull() && jR.isNumber()) {
						Integer receiver = Integer.valueOf(jR.getAsInt());
						JsonArray datas = je.getAsJsonObject().getAsJsonArray("Datas");
						JsonArray amounts = je.getAsJsonObject().getAsJsonArray("Amounts");
						JsonPrimitive exp = je.getAsJsonObject().getAsJsonPrimitive("Exp");
						JsonPrimitive cash = je.getAsJsonObject().getAsJsonPrimitive("Cash");
						Iterator dI = datas.iterator();
						Iterator aI = amounts.iterator();
						OrderEntry oe = new OrderEntry(level, receiver, Integer.valueOf(this.orders.size()));
						oe.setCachExp(Integer.valueOf(cash.getAsInt()), Integer.valueOf(exp.getAsInt()));

						while(dI.hasNext()) {
							JsonElement jId = (JsonElement)dI.next();
							JsonElement jAmount = (JsonElement)aI.next();
							if(!jId.isJsonNull() && !jAmount.isJsonNull()) {
								oe.addItem(Integer.valueOf(jId.getAsInt()), Integer.valueOf(jAmount.getAsInt()));
							}
						}

						this.orders.addOrder(oe);
					} else {
						this.orders.addEmptyOrder(level);
					}
				}
			}
		}

		this.orders.sort();
	}

	private void checkHoneycomb(JsonElement e, int instance) {
		JsonObject p = e.getAsJsonObject();
		int oId = p.getAsJsonPrimitive("ID").getAsInt();
		JsonPrimitive jp = p.getAsJsonPrimitive("GatheredResource");
		int resource = 0;
		if(jp != null && !jp.isJsonNull() && jp.isNumber()) {
			resource = p.getAsJsonPrimitive("GatheredResource").getAsInt();
		}

		ProductionFactories pcfg = (ProductionFactories)this.productions.get(Integer.valueOf(GlobalID.create(150, 0)));
		if(pcfg != null) {
			DisplayProductionObject dpo = new DisplayProductionObject(oId, 1, instance);
			dpo.setGatheredResource(resource);
			this.Honeycomb.add(dpo);
		}
	}

	private void checkNectars(JsonElement e, int instance) {
		JsonObject p = e.getAsJsonObject();
		int oId = p.getAsJsonPrimitive("ID").getAsInt();
		ProductionFactories pcfg = (ProductionFactories)this.productions.get(Integer.valueOf(oId));
		if(pcfg != null) {
			NectarTreeEntry treeEntry = NectarTrees.getById(Integer.valueOf(oId));
			if(treeEntry != null) {
				boolean isFruitsPresent = false;
				Integer x = null;
				Integer y = null;
				Boolean mirrored = null;
				JsonPrimitive jnumber = p.getAsJsonPrimitive("X");
				if(jnumber != null && !jnumber.isJsonNull() && jnumber.isNumber()) {
					x = Integer.valueOf(jnumber.getAsInt());
				}

				jnumber = p.getAsJsonPrimitive("Y");
				if(jnumber != null && !jnumber.isJsonNull() && jnumber.isNumber()) {
					y = Integer.valueOf(jnumber.getAsInt());
				}

				jnumber = p.getAsJsonPrimitive("Mirrored");
				if(jnumber != null && !jnumber.isJsonNull() && jnumber.isBoolean()) {
					mirrored = Boolean.valueOf(jnumber.getAsBoolean());
				}

				int rank = treeEntry.getFruitCount().intValue();
				DisplayProductionObject dpo = new DisplayProductionObject(oId, rank, instance);
				if(x != null) {
					dpo.setX(x);
				}

				if(y != null) {
					dpo.setY(y);
				}

				if(mirrored != null) {
					dpo.setMirroed(mirrored);
				}

				JsonArray fruits = p.getAsJsonArray("Fruits");
				if(fruits != null && fruits.isJsonArray() && !fruits.isJsonNull()) {
					isFruitsPresent = true;
					Integer np = Integer.valueOf(0);

					for(Iterator var17 = fruits.iterator(); var17.hasNext(); np = Integer.valueOf(np.intValue() + 1)) {
						JsonElement f = (JsonElement)var17.next();
						if(f.getAsBoolean()) {
							dpo.addRemoved(148);
							dpo.addRemovedIndex(np.intValue());
						} else {
							dpo.addFinished(148);
							dpo.addFinishedIndex(np.intValue());
						}
					}
				}

				NectarProduction np1 = (NectarProduction)this.nectar.get(treeEntry.getName());
				if(np1 != null) {
					if(!isFruitsPresent) {
						np1.addRedyToRemove(dpo);
					} else {
						np1.add(dpo);
					}
				}
			}

		}
	}

	private void checkMysteryBox(JsonElement e) {
		boolean isLocked = false;
		boolean LockedChecked = false;
		if(e.isJsonObject()) {
			JsonObject box = e.getAsJsonObject();
			Integer GoodGlobalId = Integer.valueOf(box.getAsJsonPrimitive("GoodGlobalId").getAsInt());
			Integer GoodAmount = Integer.valueOf(box.getAsJsonPrimitive("GoodAmount").getAsInt());
			Integer DiamondCost = Integer.valueOf(box.getAsJsonPrimitive("DiamondCost").getAsInt());
			JsonPrimitive l = box.getAsJsonPrimitive("LockedChecked");
			JsonPrimitive p = box.getAsJsonPrimitive("Locked");
			if(l != null && !l.isJsonNull() && l.isBoolean()) {
				LockedChecked = l.getAsBoolean();
			}

			if(!LockedChecked) {
				this.OpenBox = new MysteryBox(MysteryBoxState.NotChecked, GoodGlobalId.intValue(), GoodAmount.intValue(), DiamondCost.intValue());
				return;
			}

			if(p != null && !p.isJsonNull() && p.isBoolean()) {
				isLocked = p.getAsBoolean();
			}

			if(this.OpenBox == null) {
				this.OpenBox = new MysteryBox(MysteryBoxState.NotChecked, GoodGlobalId.intValue(), GoodAmount.intValue(), DiamondCost.intValue());
			}

			if(isLocked) {
				this.OpenBox.setState(MysteryBoxState.Locked);
			} else {
				this.OpenBox.setState(MysteryBoxState.Unlocked);
			}
		}

	}

	private void showFields() {
		LinkedHashMap f = new LinkedHashMap();
		Iterator var3 = Field.getIds().iterator();

		while(var3.hasNext()) {
			Integer fieldId = (Integer)var3.next();
			String key = "\"" + Field.getNameById(fieldId.intValue()) + "\"";
			int count = this.getFieldsCountWithId(this.Fields, fieldId.intValue());
			if(count > 0) {
				f.put(key, Integer.valueOf(count));
			}
		}

		System.out.println(Logger.formatP(this.logName, "  Fields: " + f));
	}

	public void showBarnSilosLevels() {
		System.out.println(Logger.formatPT(this.logName, "  SILOS: " + this.getSilosCount(this.inventory) + "/" + this.getSilosMax(this.inventory)));
		System.out.println(Logger.formatPT(this.logName, "  BARN: " + this.getBarnCount(this.inventory) + "/" + this.getBarnMax(this.inventory)));
		System.out.println(Logger.formatPT(this.logName, "  Level:" + this.currentLevel + ", Experience:" + this.currentExp + "/" + ExpLevels.getExpToNextLevel(this.currentLevel.intValue()) + ", Coins:" + this.currentCoins + ", Crystalls:" + this.currentCrystalls));
	}

	private int getFieldsCountWithId(List<DisplayObject> Fields, int id) {
		int count = 0;
		Iterator var5 = Fields.iterator();

		while(var5.hasNext()) {
			DisplayObject hddo = (DisplayObject)var5.next();
			if(hddo.id() == id) {
				++count;
			}
		}

		return count;
	}

	public int getNextFieldID() {
		int val = this.FreeFieldID.intValue();
		this.FreeFieldID = Integer.valueOf(this.FreeFieldID.intValue() + 1);
		return val;
	}

	public int getNextFruitTreeID() {
		return (this.FreeFruitTreeID = Integer.valueOf(this.FreeFruitTreeID.intValue() + 1)).intValue();
	}

	public int getNextNectarTreeID() {
		return (this.FreeNectarTreeID = Integer.valueOf(this.FreeNectarTreeID.intValue() + 1)).intValue();
	}

	public int getNextConstructionID() {
		return (this.FreeConstructionID = Integer.valueOf(this.FreeConstructionID.intValue() + 1)).intValue();
	}

	public List<DisplayObject> getFields() {
		return this.Fields;
	}

	public LinkedHashMap<Integer, Integer> readyToCompleteBuildingExp() {
		return this.readyToCompleteBuildingExp;
	}

	public LinkedHashMap<Integer, ConstructionObject> constructionBuildings() {
		return this.constructionBuildings;
	}

	public int speedupCrops(EndClientTurn ectm, int lastTick, boolean showlog) {
		int cmdcount = 0;
		String nts = new String();
		Iterator var7 = this.Fields.iterator();

		while(var7.hasNext()) {
			DisplayObject curr = (DisplayObject)var7.next();
			if(curr.id() != 400000 && curr.timer() != null && curr.timer().getRmainingSeconds(lastTick) > 0) {
				curr.timer().reset();
				ectm.addLogicCommand(new SpeedUpCrops(lastTick, curr.field()));
				lastTick += 2;
				++cmdcount;
				nts = nts + "\"" + Field.getNameById(curr.id()) + "\" ";
			}
		}

		if(showlog && nts.length() > 0) {
			System.out.println(Logger.formatPT(this.logName, "  SpeedUp: [ " + nts + "]"));
		}

		return cmdcount;
	}

	public int sowSeeds(int tid, int count, int timerTimeSec, EndClientTurn ectm, int lastTick, boolean logOption) {
		int sowcount = 0;
		Integer tidCount = (Integer)this.inventory.get(Integer.valueOf(tid));
		if(tidCount != null) {
			if(tidCount.intValue() < count) {
				count = tidCount.intValue();
			}

			int cticks = 0;
			String nts = new String();

			for(int cs = 0; cs < count; ++cs) {
				Iterator var13 = this.Fields.iterator();

				while(var13.hasNext()) {
					DisplayObject curr = (DisplayObject)var13.next();
					if(curr.id() == 400000) {
						curr.setId(tid);
						curr.setTimer(new DisplayObjectTimer(timerTimeSec, Utils.secondsToObjectTicks(timerTimeSec), lastTick + cticks));
						ectm.addLogicCommand(new SowSeeds(lastTick + cticks, curr.field(), tid));
						cticks += 2;
						curr.setField(this.getNextFieldID());
						++sowcount;
						if(logOption) {
							nts = nts + "\"" + Field.getNameById(curr.id()) + "\" ";
						}
						break;
					}
				}
			}

			if(nts.length() > 0) {
				System.out.println(Logger.formatPT(this.logName, "  SowSeeds: [ " + nts + "]"));
			}
		}

		if(sowcount > 0) {
			this.inventory.put(Integer.valueOf(tid), Integer.valueOf(tidCount.intValue() - sowcount));
		}

		return sowcount;
	}

	public int collectHarvest(EndClientTurn ectm, int lastTick, int cmdcount, int maxharvest, boolean logOption) {
		LinkedHashMap needToCollect = new LinkedHashMap();
		int silosMax = this.getSilosMax(this.inventory);
		int cticks = 0;

		for(int harvested = 0; harvested < this.Fields.size(); --maxharvest) {
			DisplayObject key = (DisplayObject)this.Fields.get(harvested);
			Integer fe = (Integer)this.inventory.get(Integer.valueOf(key.id()));
			int newcount = 0;
			if(fe != null) {
				newcount = fe.intValue();
			}

			boolean needHarvesting = false;
			int silosCount = this.getSilosCount(this.inventory);
			int remaining = 0;
			if(key.timer() != null) {
				remaining = key.timer().getRmainingSeconds(lastTick + cticks);
			}

			if(key.id() > 400000 && remaining == 0) {
				if(silosCount + 2 > silosMax || maxharvest <= 0) {
					break;
				}

				needHarvesting = true;
			}

			if(needHarvesting) {
				++this.harvestCount;
				needToCollect.put(Integer.valueOf(key.field()), Field.getNameById(key.id()));
				newcount += 2;
				this.inventory.put(Integer.valueOf(key.id()), Integer.valueOf(newcount));
				key.setId(400000);
				key.setField(this.getNextFieldID());
				cmdcount += 3;
				cticks += 2;
				if(cmdcount >= 48) {
					break;
				}
			}

			++harvested;
		}

		Integer var16;
		Iterator var17;
		for(var17 = needToCollect.keySet().iterator(); var17.hasNext(); lastTick += 2) {
			var16 = (Integer)var17.next();
			ectm.addLogicCommand(new HarvestField(lastTick, var16.intValue()));
		}

		for(var17 = needToCollect.keySet().iterator(); var17.hasNext(); lastTick += 2) {
			var16 = (Integer)var17.next();
			ectm.addLogicCommand(new HarvestFieldGain(lastTick, var16.intValue()));
		}

		var17 = needToCollect.keySet().iterator();

		while(var17.hasNext()) {
			var16 = (Integer)var17.next();
			ectm.addLogicCommand(new HarvestFieldOver(lastTick, var16.intValue()));
			lastTick += 2;
			FieldsEntry var19 = Field.getEntryByName((String)needToCollect.get(var16));
			if(var19 != null) {
				this.updateCurrentExp(var19.getExpCollect());
			}
		}

		if(logOption && needToCollect.size() > 0) {
			String var18 = new String();

			String var20;
			for(Iterator var21 = needToCollect.values().iterator(); var21.hasNext(); var18 = var18 + "\"" + var20 + "\" ") {
				var20 = (String)var21.next();
			}

			System.out.println(Logger.formatPT(this.logName, "  Harvest: [ " + var18 + "]"));
		}

		return cmdcount;
	}

	public int collectHarvest(EndClientTurn ectm, int lastTick, int cmdcount, boolean logOption) {
		return this.collectHarvest(ectm, lastTick, cmdcount, 10000, logOption);
	}

	private boolean checkNeighborAccept(int goodid, int count) {
		return GlobalID.getClassID(goodid) != 22?(GlobalID.create(4, 1) == goodid?Utils.canUse(goodid, count, this.inventory, false):Utils.canUse(goodid, count, this.inventory, true)):false;
	}

	public int forestSlot(EndClientTurn ectm, int lastTick, boolean showlog) {
		int cmdcount = 0;
		ArrayList ready = new ArrayList();
		Iterator var7 = this.forests.getForestsToRemove().iterator();

		ForestsInstanceEntry e;
		while(var7.hasNext()) {
			e = (ForestsInstanceEntry)var7.next();
			Integer fe = (Integer)this.inventory.get(e.getToolId());
			if(fe != null && fe.intValue() > 0) {
				ready.add(e);
				this.inventory.put(e.getToolId(), Integer.valueOf(fe.intValue() - 1));
			}

			if(ready.size() >= 2) {
				break;
			}
		}

		var7 = ready.iterator();

		while(var7.hasNext()) {
			e = (ForestsInstanceEntry)var7.next();
			ectm.addLogicCommand(new ForestsUseTool(lastTick, e.getInstanceId()));
			lastTick += 130;
			ectm.addLogicCommand(new ForestsRemove(lastTick, e.getInstanceId()));
			lastTick += 2;
			cmdcount += 2;
			this.forests.removeEntry(e);
			ForestsEntry fe1 = Forests.getById(Integer.valueOf(e.getObjectId()));
			if(showlog && fe1 != null) {
				System.out.println(Logger.formatPT(this.logName, "  Forests slot:remove \"" + fe1.getName() + "\""));
			}
		}

		return cmdcount;
	}

	public int neighborsSlot(EndClientTurn ectm, int lastTick, boolean showlog) {
		int cmdcount = 0;
		if(this.neighbors.size() > 0) {
			boolean doAction = true;
			Iterator n = this.neighbors.iterator();

			while(n.hasNext()) {
				Neighbor idx = (Neighbor)n.next();
				if(idx.updateIdle() != 0) {
					doAction = false;
				}
			}

			if(doAction) {
				for(int var10 = 0; var10 < this.neighbors.size(); ++var10) {
					Neighbor var11 = (Neighbor)this.neighbors.get(var10);
					boolean accepted = this.checkNeighborAccept(var11.GoodGlobalId(), var11.GoodAmount());
					if(accepted) {
						lastTick += 128;
						ectm.addLogicCommand(new NeighborsSellAccept(lastTick, GlobalID.create(49, var10)));
						int count = ((Integer)this.inventory.get(Integer.valueOf(var11.GoodGlobalId()))).intValue();
						this.inventory.put(Integer.valueOf(var11.GoodGlobalId()), Integer.valueOf(count - var11.GoodAmount()));
					} else {
						lastTick += 128;
						ectm.addLogicCommand(new NeighborsSellCancel(lastTick + 32, GlobalID.create(49, var10)));
					}

					if(showlog) {
						System.out.println(Logger.formatPT(this.logName, "  Neighbor:" + StaticData.getGlobalName(Integer.valueOf(var11.id())) + " GoodGlobalID:" + "\"" + StaticData.getGlobalName(Integer.valueOf(var11.GoodGlobalId())) + "\"" + " GoodAmount:" + var11.GoodAmount() + " sell " + (accepted?"accepted":"rejected")));
					}

					++cmdcount;
				}

				this.neighbors.clear();
			}
		}

		return cmdcount;
	}

	public int rssClaimSlot(EndClientTurn ectm, int lastTick, boolean showlog) {
		int cmdcount = 0;
		int profit = 0;

		for(int i = 0; i < this.shop.maxSlot() && cmdcount < 30; ++i) {
			RoadsideShopSlot s = this.shop.getSlot(i);
			if(!s.isSlotEmpty() && s.isSold()) {
				ectm.addLogicCommand(new ClaimRoadsideShopStand(lastTick, i));
				lastTick += 2;
				++cmdcount;
				profit += s.price();
				s.clear();
				if(showlog) {
					System.out.println(Logger.formatPT(this.logName, "  RSS: Claim slot " + i));
				}
			}
		}

		if(showlog && profit > 0) {
			System.out.println(Logger.formatPT(this.logName, "  RSS Current Profit: " + profit));
		}

		return cmdcount;
	}

	private int getSiloSafeCount(Integer cropid, Integer mainCrop) {
		if(cropid.intValue() == mainCrop.intValue()) {
			int crops = this.getFieldsCountWithId(this.Fields, cropid.intValue()) * 2;
			return crops >= this.Fields.size()?0:this.Fields.size() - crops;
		} else {
			return 0;
		}
	}

	public int sellSlot(EndClientTurn ectm, int lastTick, LinkedHashMap<Integer, List<Integer>> tosell, boolean sellPriceMin, JsonObject jshop) {
		int cmdcount = 0;
		if(this.currentLevel.intValue() < 7) {
			return 0;
		} else {
			JsonArray jslots = new JsonArray();
			Iterator var9 = tosell.keySet().iterator();

			while(var9.hasNext()) {
				Integer si = (Integer)var9.next();
				Integer object = (Integer)((List)tosell.get(si)).get(0);
				Integer count = (Integer)((List)tosell.get(si)).get(1);
				Integer oc = (Integer)this.inventory.get(object);
				if(oc != null && oc.intValue() >= count.intValue()) {
					Integer price = StaticData.getPriceById(object);
					if(price != null) {
						Integer slotPrice;
						if(sellPriceMin) {
							slotPrice = Integer.valueOf(1);
						} else {
							slotPrice = Integer.valueOf(price.intValue() * count.intValue());
						}

						RoadsideShopSlot s = this.shop.getSlot(si.intValue());
						s.update(object.intValue(), slotPrice.intValue(), count.intValue(), false);
						jslots.add(s.getAsJsonObject());
						ectm.addLogicCommand(new PutStuffToRoadsideShopStand(lastTick, si.intValue(), object.intValue(), count.intValue(), slotPrice.intValue(), 0));
						++cmdcount;
						lastTick += 2;
						this.inventory.put(object, Integer.valueOf(oc.intValue() - count.intValue()));
						System.out.println(Logger.formatPT(this.logName, "  Put \"" + StaticData.getGlobalName(object) + "\" to shop slot:" + si + " (count:" + count + " price:" + slotPrice + ")"));
					}
				}
			}

			if(jslots.size() > 0) {
				jshop.add("putstuff", jslots);
			}

			return cmdcount;
		}
	}

	public int sellSlot(EndClientTurn ectm, int lastTick, List<Integer> tosell, boolean sellPriceMin, Integer mainCop, boolean showlog) {
		int cmdcount = 0;
		if(this.currentLevel.intValue() < 7) {
			return 0;
		} else {
			int sellPackCount = this.getBarnMax(this.inventory) >= 3000?10:1;
			Iterator var10 = tosell.iterator();

			while(var10.hasNext()) {
				Integer object = (Integer)var10.next();
				if(cmdcount >= 30) {
					break;
				}

				Integer safeCount = Integer.valueOf(0);
				if(GlobalID.getClassID(object.intValue()) == 4) {
					safeCount = Integer.valueOf(this.getSiloSafeCount(object, mainCop));
				}

				List emptySlots = this.getShopEmptySlots();
				if(emptySlots.size() < 3) {
					break;
				}

				Iterator var14 = emptySlots.iterator();

				while(var14.hasNext()) {
					Integer si = (Integer)var14.next();
					if(this.getShopEmptySlotsCount() < 3) {
						break;
					}

					Integer oc = (Integer)this.inventory.get(object);
					if(oc == null || oc.intValue() < sellPackCount || cmdcount >= 30) {
						break;
					}

					int mc = oc.intValue();
					int count = mc > 10?10:mc;
					if(count + safeCount.intValue() <= mc) {
						Integer price = StaticData.getPriceById(object);
						if(price != null) {
							Integer slotPrice;
							if(sellPriceMin) {
								slotPrice = Integer.valueOf(1);
							} else {
								slotPrice = Integer.valueOf(price.intValue() * count);
							}

							RoadsideShopSlot s = this.shop.getSlot(si.intValue());
							s.update(object.intValue(), slotPrice.intValue(), count, false);
							ectm.addLogicCommand(new PutStuffToRoadsideShopStand(lastTick, si.intValue(), object.intValue(), count, slotPrice.intValue(), 0));
							++cmdcount;
							lastTick += 2;
							this.inventory.put(object, Integer.valueOf(mc - count));
							if(showlog) {
								System.out.println(Logger.formatPT(this.logName, "  Put \"" + StaticData.getGlobalName(object) + "\" to shop slot:" + si + " (count:" + count + " price:" + slotPrice + ")"));
							}
						} else if(showlog) {
							System.out.println(Logger.formatPT(this.logName, "  Unable to Put \"" + StaticData.getGlobalName(object) + "\" to shop slot:" + si + " (count:" + count + " price:" + 0 + ")"));
						}
					}
				}
			}

			return cmdcount;
		}
	}

	public int rssAdvertiseSlot(EndClientTurn ectm, int lt, boolean showlog) {
		int cmdcount = 0;
		if(this.freeAdTimer != null && this.freeAdTimer.getRmainingSeconds(lt) == 0) {
			int ai = this.LastAdvertiseIndex;
			if(ai >= this.shop.maxSlot()) {
				ai = 0;
			}

			for(int countIndex = 0; countIndex < this.shop.maxSlot(); ++countIndex) {
				RoadsideShopSlot s = this.shop.getSlot(ai);
				if(!s.isSlotEmpty() && !s.isSold() && !s.isAdvertise()) {
					if(cmdcount < 30) {
						s.setAdvertise();
						this.freeAdTimer = new DisplayObjectTimer(300, Utils.secondsToObjectTicks(300), lt);
						ectm.addLogicCommand(new NewspaperAdvertise(lt, ai));
						++cmdcount;
						int lastTick = lt + 2;
						if(showlog) {
							System.out.println(Logger.formatPT(this.logName, "  RSS: Set Advertise flag to slot " + ai));
						}
					}

					this.LastAdvertiseIndex = ai;
					break;
				}

				++ai;
				if(ai == this.shop.maxSlot()) {
					ai = 0;
				}
			}

			this.LastAdvertiseIndex = ai;
		}

		return cmdcount;
	}

	public List<Integer> getShopEmptySlots() {
		ArrayList val = new ArrayList();

		for(int si = 0; si < this.shop.maxSlot(); ++si) {
			if(this.shop.getSlot(si).isSlotEmpty()) {
				val.add(Integer.valueOf(si));
			}
		}

		return val;
	}

	public int getShopEmptySlotsCount() {
		int count = 0;

		for(int si = 0; si < this.shop.maxSlot(); ++si) {
			RoadsideShopSlot s = this.shop.getSlot(si);
			if(s.isSlotEmpty()) {
				++count;
			}
		}

		return count;
	}

	public int silosUpgradeSlot(EndClientTurn ectm, int lastTick, boolean showlog) {
		this.silosUpgradeUpdateResources();
		ectm.addLogicCommand(new UpgradeBuilding(lastTick, GlobalID.create(18, 0)));
		if(showlog) {
			System.out.println(Logger.formatPT(this.logName, "  SILOS Upgraded to level " + (this.getNextSilosLevel(this.inventory) - 1)));
		}

		return 1;
	}

	public int barnUpgradeSlot(EndClientTurn ectm, int lastTick, boolean showlog) {
		this.barnUpgradeUpdateResources();
		ectm.addLogicCommand(new UpgradeBuilding(lastTick, GlobalID.create(14, 0)));
		if(showlog) {
			System.out.println(Logger.formatPT(this.logName, "  BARN Upgraded to level " + (this.getNextBarnLevel(this.inventory) - 1)));
		}

		return 1;
	}

	private List<Integer> getBarnIds() {
		ArrayList barnIds = new ArrayList();
		barnIds.add(SilosBarnMaterials.getGlobalID(WareHouse.getTool1()));
		barnIds.add(SilosBarnMaterials.getGlobalID(WareHouse.getTool2()));
		barnIds.add(SilosBarnMaterials.getGlobalID(WareHouse.getTool3()));
		return barnIds;
	}

	public boolean checkBarnUpgrade() {
		int barnLevel = this.getNextBarnLevel(this.inventory);
		List barnIds = this.getBarnIds();
		boolean doUpgrade = true;
		Iterator var5 = barnIds.iterator();

		while(true) {
			Integer count;
			do {
				if(!var5.hasNext()) {
					return doUpgrade;
				}

				Integer id = (Integer)var5.next();
				count = (Integer)this.inventory.get(id);
			} while(count != null && count.intValue() >= barnLevel);

			doUpgrade = false;
		}
	}

	public boolean checkBarnUpgrade(LinkedHashMap<Integer, Integer> needToUpgrade, LinkedHashMap<Integer, Integer> canSell) {
		boolean doUpgrade = this.checkBarnUpgrade();
		this.fillBarnBySell(needToUpgrade, canSell, doUpgrade);
		return doUpgrade;
	}

	public void barnUpgradeUpdateResources() {
		int barnLevel = this.getNextBarnLevel(this.inventory);
		List barnIds = this.getBarnIds();
		Iterator var4 = barnIds.iterator();

		while(var4.hasNext()) {
			Integer id = (Integer)var4.next();
			Integer count = (Integer)this.inventory.get(id);
			this.inventory.put(id, Integer.valueOf(count.intValue() - barnLevel));
		}

		this.inventory.put(Integer.valueOf(1900007), Integer.valueOf(barnLevel + 1));
	}

	private List<Integer> getSilosIds() {
		ArrayList silosIds = new ArrayList();
		silosIds.add(SilosBarnMaterials.getGlobalID(Silos.getTool1()));
		silosIds.add(SilosBarnMaterials.getGlobalID(Silos.getTool2()));
		silosIds.add(SilosBarnMaterials.getGlobalID(Silos.getTool3()));
		return silosIds;
	}

	public boolean checkSilosUpgrade() {
		int barnLevel = this.getNextSilosLevel(this.inventory);
		List silosIds = this.getSilosIds();
		boolean doUpgrade = true;
		Iterator var5 = silosIds.iterator();

		while(true) {
			Integer count;
			do {
				if(!var5.hasNext()) {
					return doUpgrade;
				}

				Integer id = (Integer)var5.next();
				count = (Integer)this.inventory.get(id);
			} while(count != null && count.intValue() >= barnLevel);

			doUpgrade = false;
		}
	}

	public boolean checkSilosUpgrade(LinkedHashMap<Integer, Integer> needToUpgrade, LinkedHashMap<Integer, Integer> canSell) {
		boolean doUpgrade = this.checkSilosUpgrade();
		this.fillSilosBySell(needToUpgrade, canSell, doUpgrade);
		return doUpgrade;
	}

	public void silosUpgradeUpdateResources() {
		int silosLevel = this.getNextSilosLevel(this.inventory);
		List silosIds = this.getSilosIds();
		Iterator var4 = silosIds.iterator();

		while(var4.hasNext()) {
			Integer id = (Integer)var4.next();
			Integer count = (Integer)this.inventory.get(id);
			this.inventory.put(id, Integer.valueOf(count.intValue() - silosLevel));
		}

		this.inventory.put(Integer.valueOf(1900006), Integer.valueOf(silosLevel + 1));
	}

	private void fillBarnBySell(LinkedHashMap<Integer, Integer> needToUpgrade, LinkedHashMap<Integer, Integer> canSell, boolean doUpgrade) {
		List ids = this.getBarnIds();
		int level = this.getNextBarnLevel(this.inventory);
		this.fillBuySell(needToUpgrade, canSell, ids, level, doUpgrade);
	}

	private void fillSilosBySell(LinkedHashMap<Integer, Integer> needToUpgrade, LinkedHashMap<Integer, Integer> canSell, boolean doUpgrade) {
		List ids = this.getSilosIds();
		int level = this.getNextSilosLevel(this.inventory);
		this.fillBuySell(needToUpgrade, canSell, ids, level, doUpgrade);
	}

	private void fillBuySell(LinkedHashMap<Integer, Integer> needToUpgrade, LinkedHashMap<Integer, Integer> canSell, List<Integer> ids, int level, boolean doUpgrade) {
		Integer id;
		Iterator var7;
		Integer count;
		Integer needed;
		if(doUpgrade) {
			var7 = ids.iterator();

			while(var7.hasNext()) {
				id = (Integer)var7.next();
				count = (Integer)this.inventory.get(id);
				needed = Integer.valueOf(count.intValue() - level * 2 - 1);
				if(needed.intValue() > 0) {
					canSell.put(id, needed);
				}
			}
		} else {
			var7 = ids.iterator();

			while(var7.hasNext()) {
				id = (Integer)var7.next();
				count = (Integer)this.inventory.get(id);
				if(count == null) {
					count = Integer.valueOf(0);
				}

				needed = Integer.valueOf(level - count.intValue());
				if(needed.intValue() > 0) {
					needToUpgrade.put(id, needed);
				} else {
					Integer unneeded = Integer.valueOf(count.intValue() - level * 2 - 1);
					if(unneeded.intValue() > 0) {
						canSell.put(id, unneeded);
					}
				}
			}
		}

	}

	public Integer getHarvestCount() {
		return Integer.valueOf(this.harvestCount);
	}

	public Integer getCurrentLevel() {
		return this.currentLevel;
	}

	public Integer getCurrentCoins() {
		return this.currentCoins;
	}

	public Integer getCurrentExp() {
		return this.currentExp;
	}

	public Integer getCurrentCrystalls() {
		return this.currentCrystalls;
	}

	public List<Neighbor> getNeighbors() {
		return this.neighbors;
	}

	public RoadsideShop getShop() {
		return this.shop;
	}

	public void updateShop(int slotCount) {
		this.shop = new RoadsideShop(slotCount);
		this.shop.initEmptyShop();
	}

	public ForestsInstance getForests() {
		return this.forests;
	}

	public LinkedHashMap<Integer, Integer> getInventory() {
		return this.inventory;
	}

	public int getSilosEmpty() {
		return this.getSilosEmptyCount(this.inventory);
	}

	public int getBarnEmpty() {
		return this.getBarnEmptyCount(this.inventory);
	}

	public MysteryBox getOpenBox() {
		return this.OpenBox;
	}

	public void clearOpenBox() {
		this.OpenBox = null;
	}

	public LinkedHashMap<Integer, ProductionFactories> getActiveProductions() {
		return this.activeProductions;
	}

	public int getSilosMax() {
		return this.getSilosMax(this.inventory);
	}

	public HoneycombProduction getHoneycomb() {
		return this.Honeycomb;
	}

	public void updateInventoryId(Integer id, Integer amount) {
		Integer count = (Integer)this.inventory.get(id);
		if(count == null) {
			count = Integer.valueOf(0);
		}

		this.inventory.put(id, Integer.valueOf(count.intValue() + amount.intValue()));
	}
}
